package org.example.myleet.rosalind.pdst;

import org.example.myleet.rosalind.utils.FastaReader;
import org.example.myleet.rosalind.utils.FastaSequence;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String path = solution.getClass().getResource("").getPath() + "example.txt";
        solution.printDistanceMatrix(FastaReader.parseFastaFromFile(path));
    }

    public void printDistanceMatrix(List<FastaSequence> sequences) {
        for (int i = 0; i < sequences.size(); ++i) {
            FastaSequence seqI = sequences.get(i);
            for (int j = 0; j < sequences.size(); ++j) {
                System.out.print(hammingDistanceProportion(seqI.getSequence(), sequences.get(j).getSequence()));
                if (j < sequences.size() - 1) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

    private BigDecimal hammingDistanceProportion(String seq1, String seq2) {
        int distance = 0;
        for (int i = 0; i < seq1.length(); ++i) {
            if (seq1.charAt(i) != seq2.charAt(i)) {
                ++distance;
            }
        }
        return BigDecimal.valueOf(distance).divide(BigDecimal.valueOf(seq1.length()), 5, RoundingMode.HALF_UP);
    }
}
